<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="目录名称" prop="categoryName">
        <el-input v-model="dataForm.categoryName" placeholder="目录名称"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="categoryDesc">
        <el-input v-model="dataForm.categoryDesc" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item label="优先级别" prop="priority">
        <el-input v-model="dataForm.priority" placeholder="优先级别"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        categoryId: 0,
        storeId: 0,
        categoryName: "",
        categoryDesc: "",
        priority: "",
      },
      dataRule: {
        categoryName: [
          { required: true, message: "目录名称不能为空", trigger: "blur" },
        ],
        categoryDesc: [
          { required: true, message: "描述不能为空", trigger: "blur" },
        ],
        priority: [
          { required: true, message: "优先级别不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id,storeId) {
      this.dataForm.categoryId = id || 0;
      this.dataForm.storeId = storeId || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.categoryId) {
          this.$http
            .get(
              `${process.env.URL_HEAD}/store/category/info/${this.dataForm.categoryId}`,
              {
                withCredentials: true,
              }
            )
            .then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.storeId = data.category.storeId;
                this.dataForm.categoryName = data.category.categoryName;
                this.dataForm.categoryDesc = data.category.categoryDesc;
                this.dataForm.priority = data.category.priority;
              }
            });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http
            .post(
              `${process.env.URL_HEAD}/store/category/${
                !this.dataForm.categoryId ? "save" : "update"
              }`,
              {
                categoryId: this.dataForm.categoryId || undefined,
                storeId: this.dataForm.storeId,
                categoryName: this.dataForm.categoryName,
                categoryDesc: this.dataForm.categoryDesc,
                priority: this.dataForm.priority,
              },
              { withCredentials: true }
            )
            .then(({ data }) => {
              if (data && data.code === 0) {
                // console.log("保存成功",data);
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
        }
      });
    },
  },
};
</script>
